<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Glbusinessunit_model extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
    }

    public function insertData($data){

      $this->db->query("call gl_business_unit_ins(?,?,?,?,?,?,?,?,@id,@err,@msg)",$data);
        $que = $this->db->query("SELECT @id as id,@err as err,@msg as msg");
        $id; $msg; $err;
          foreach($que->result() as $keys){
            $msg = $keys->msg;
            $err = $keys->err;
            $id = $keys->id;
          }
          $arr = array(
              'msg'   =>$msg,
              'err'   =>$err,
              'id'  =>$id
          );
        echo json_encode($arr);

    }

    public function updateData($data){

      $this->db->query("call gl_business_unit_upd(?,?,?,?,?,?,?,?,@err,@msg)",$data);
        $que = $this->db->query("SELECT @err as err,@msg as msg");
        $id; $msg; $err;
          foreach($que->result() as $keys){
            $msg = $keys->msg;
            $err = $keys->err;
          }
          $arr = array(
              'msg'   =>$msg,
              'err'   =>$err
          );
        echo json_encode($arr);

    }

    public function searchData($code,$name){
        $sql;
        if($code != "" && $name != ""){
          $sql = "SELECT * FROM gl_business_units_v WHERE bu_code LIKE ".$this->db->escape($code)." AND bu_name LIKE '".$name."' ";
        }else if($code == "" && $name != ""){
          $sql = "SELECT * FROM gl_business_units_v WHERE bu_name LIKE ".$this->db->escape($name)." ";
        }else{
          $sql = "SELECT * FROM gl_business_units_v WHERE bu_code LIKE ".$this->db->escape($code)." ";
        }
        //$query = $this->db->get_where('gl_business_unit_v', array('bu_code' => $code, 'bu_name' => $name), 1);
        $query = $this->db->query($sql);
        if($query->num_rows() != 0){
          return $query->result();
        }
        else {
          $result = "No results found.";
          return $result;
        }
    }

    public function getBu($id){
      $this->db->where('bu_id',$id);
      $query = $this->db->get('gl_business_units_v');
      return $query->result();
    }

    public function get_cogs(){
        $query = $this->db->query("SELECT * FROM gl_cogs_acct_v");
        return $query->result(); 
    }

    public function get_expense(){
        $query = $this->db->query("SELECT * FROM gl_expense_acct_v");
     return $query->result(); 
    }

    public function get_sales(){
        $query = $this->db->query("SELECT * FROM gl_sales_acct_v");
     return $query->result(); 
    }
}